<?php 
namespace app\admin\controller;

class Login extends Comm{
  //登录
  public function login(){
    if(request()->isPost()){
      $data = [
        'uname'=>input('post.uname'),
        'pwd'=>input('post.pwd','','md5'),
      ];
      $count = model('Member')->where($data)->count();
      if($count == 1){
        session('uname',input('post.uname'));
        $this->success('成功','/admin/index/index');
      }else{
        $this->error('失败');
      }
    }else{
      return $this->fetch(); 
    }  
  }

  //处理登录
  public function do_login(){
    $vld = $this->validate(input('post.'),'Member.login');
    if($vld !== true){
      $this->error($vld);
    }
    // $this->success(input('post.'));exit;
    $where1 = ['utel'=>input('post.user'),'pwd'=>md5(input('post.pwd'))];
    $where2 = ['uname'=>input('post.user'),'pwd'=>md5(input('post.pwd'))];
    $count1 = model('Member')->where($where1)->count();
    $count2 = model('Member')->where($where2)->count();
    $count = $count1+$count2;
    if($count > 0){
      if($count1 > 0){
        $uname = model('Member')->where($where1)->find()->uname;
        session('uname',$uname);
      }else{
        session('uname',input('post.user'));
      }
      $this->success('成功');
    }else{
      $this->error(input('post.'));
    }
  }

  //注销
  public function logout(){
    session('uname',null);
    return $this->redirect('login');
  }


}